Split register_tool into register_attribute_tool and register_lint_tool - #158038
Conversation
|
Some changes occurred in compiler/rustc_passes/src/check_attr.rs cc @jdonszelmann, @JonathanBrouwer Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer Some changes occurred in compiler/rustc_hir/src/attrs |
|
r? @wesleywiser rustbot has assigned @wesleywiser. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
Error seems to be flaky rustdoc test: #157747 |
|
r? me (Or Jonathan I suppose) |
|
Can you expand on the "resolution yet to be implemented" thing? I was under the impression there would be no resolution and it'd all be, essentially, stringly typed. |
In https://github.com/rust-lang/rfcs/blob/master/text/3808-register-tool.md it mentioned that ambiguity between tool attribute and other resolution should be an error, and also that tool attribute is not affected by no_implicit_prelude. These are technically breaking changes so I'd want to split it to another PR so it gets a crater run. |
There was a problem hiding this comment.
Typically when there are multiple attributes which are semantically related, we "merge" them into a single attribute. See https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html#how-this-crate-works
This implementation doesn't; you'd be dealing with RegisteredTool { kind: sym::register_lint_tool, ..}, RegisteredTool { kind: sym::register_tool, ..} and RegisteredTool { kind: sym::register_attribute_tool, ..}. That's three attributes, effectively. I've pointed at some places that need changes for that.
Also, my understanding from the rfc is that resolution and ambiguity errors are only for attribute_tool? i.e. #[my_tool::thing]? and not #[allow(my_tool::lint)]?
| pub(crate) struct RegisterAttributeTool; | ||
| pub(crate) struct RegisterLintTool; | ||
| pub(crate) struct RegisterTool; | ||
|
|
||
| impl CombineAttributeParser for RegisterToolParser { | ||
| const PATH: &[Symbol] = &[sym::register_tool]; | ||
| pub(crate) trait RegisterToolKind: 'static { | ||
| const SYMBOL: Symbol; | ||
| } | ||
|
|
||
| impl RegisterToolKind for RegisterAttributeTool { | ||
| const SYMBOL: Symbol = sym::register_attribute_tool; | ||
| } | ||
|
|
||
| impl RegisterToolKind for RegisterLintTool { | ||
| const SYMBOL: Symbol = sym::register_lint_tool; | ||
| } | ||
|
|
||
| impl RegisterToolKind for RegisterTool { | ||
| const SYMBOL: Symbol = sym::register_tool; | ||
| } | ||
|
|
||
| pub(crate) struct RegisterToolParser<Kind>(Kind); | ||
|
|
||
| impl<K: RegisterToolKind> CombineAttributeParser for RegisterToolParser<K> { | ||
| const PATH: &[Symbol] = &[K::SYMBOL]; | ||
| type Item = Ident; | ||
| const CONVERT: ConvertFn<Self::Item> = |tools, _span| AttributeKind::RegisterTool(tools); | ||
| const CONVERT: ConvertFn<Self::Item> = | ||
| |tools, _span| AttributeKind::RegisterTool { kind: K::SYMBOL, tools }; |
There was a problem hiding this comment.
And this would need to implement AttributeParser directly. For example see the stability parser:
#[stable], #[unstable] and #[rustc_allowed_through_unstable_modules] into a single semantic attribute.
|
|
||
| if let Some(Attribute::Parsed(AttributeKind::RegisterTool(tools))) = | ||
| if let Some(Attribute::Parsed(AttributeKind::RegisterTool { kind: _, tools })) = | ||
| AttributeParser::parse_limited(sess, pre_configured_attrs, &[sym]) |
There was a problem hiding this comment.
Unfortunately you can only use parse_limited for one attribute at a time; you can't parse multiple attributes at a time. Feel free to make a function that allows multiple attributes though, if you need it.
| /// Tool modules introduced with `#![register_tool]`. | ||
| ToolPrelude, | ||
| /// Tool modules introduced with `#![register_tool]` or `#![register_attribute_tool]`. | ||
| ToolAttributePrelude, |
There was a problem hiding this comment.
| ToolAttributePrelude, | |
| AttributeToolPrelude, |
It's a prelude for tools, not for attributes.
I'd probably just keep the ToolPrelude naming here, because lints do no go through name resolution, but may be better to be more explicit for readers less aware of the context.
| #![register_attribute_tool(qux)] | ||
| #![register_attribute_tool(qux)] |
There was a problem hiding this comment.
@jyn514 is there a specific reason for
#![register_attribute_tool(qux)]
#![register_attribute_tool(qux)]to be allowed?
I don't see the explanation in the RFC.
Duplicate definitions are typically prohibited in name resolution.
There was a problem hiding this comment.
I am also curious on the reason. It is also strange to me that predefined tools cannot be duplicate defined. I feel that we should either allow duplicate definition for all tools (including predefined) or disallow it for all.
There was a problem hiding this comment.
I don't recall why I did this. The one use case I can think of is to have register_attribute_tool at the crate root in the source, and also with --crate-attr passed to every crate in the workspace. If we errored on duplicates, we'd disallow that, which seems unfortunate. I'm mildly in favor of allowing duplicate definition for all tools, including pre-defined.
There was a problem hiding this comment.
I have found it necessary on occasion to pass -Zcrate-attr=feature(foo) over command line when they are possibly declared in lib.rs as well. It's a deny by default lint to have duplicate features so it all just works if you pass allow(duplicate_features)
I'd expect register_tool to work similarly, but that need not be implemented in this PR
|
Naming nit: "attribute" is long, so it's usually abbreviated to "attr" in the compiler, so all the |
Please, assign me when the resolution part is submitted. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
correct, yes. (caveats here about future work around proc-macro lints, but currently today they do not require name res.) |
|
Ping @nbdd0121 Can you resolve the merge conflicts and mark it ready for review if all comments (so far) have been addressed? It won't show up in my review queue if it isn't. |
The existing API is preserved as `parse_limited_sym` as a simple helper. Similarly, `parse_limited_should_emit` is preserved as `parse_limited_sym_should_emit`. I've noted that all users targets crate, so the `target_node_id` and `target` parameters are removed from it.
There was a problem hiding this comment.
The impl looks good so far (I need to take a closer look at some parts - not done yet, will do tomorrow).
Can you add some tests for
- the feature gate on the new attributes (can add to the existing one)
-Z crate-attr = <new attributes>over cmd line (can add to the existing one)
Also some things I noticed while reviewing. They need not be fixed in this PR - best in a standalone PR - but probably before you implement the nameres part:
- register_tool is (and already was) a little too permissive in what is allowed. Things like
#![register_tool(crate)]etc - there is no test to check that register_tool doesn't work across crates
This comment has been minimized.
This comment has been minimized.
Split register_tool into register_attribute_tool and register_lint_tool
This comment was marked as outdated.
This comment was marked as outdated.
|
@rust-timer queue |
This comment was marked as outdated.
This comment was marked as outdated.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Split register_tool into register_attribute_tool and register_lint_tool
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (17a90b8): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 1.2%, secondary 0.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -1.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (secondary -0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 490.165s -> 489.724s (-0.09%) |
|
thanks for working on this :) @bors r+ rollup |
…uwer Rollup of 21 pull requests Successful merges: - #159990 (Many "predicate"-to-"clause" renamings) - #159665 (Replace most `Ty::new_fn_def` calls with `type_of` queries directly) - #159687 (rustdoc: Set tracing max_level_info when debug-logging is false) - #160057 (refactor(mir-transform): Calculate optimization status inside `run_passes_inner`) - #160060 (codegen: skip stores for entirely-uninit constant aggregate fields, attempt #2) - #160063 (Fix ICE when dumping the dep graph with the parallel frontend) - #160065 (Distinguish the dep-graph index space from the live node count) - #158038 (Split register_tool into register_attribute_tool and register_lint_tool) - #159776 (remove const hack in alloc) - #159978 (run intrinsic-test by default on x86_64-gnu) - #160008 (Avoid stale closure recovery state across statements) - #160027 (Add regression test for #132767) - #160030 (Update `browser-ui-test` version to `0.25.0`) - #160046 (Improve consistency of attribute error messages (part 2)) - #160056 (Fix associated function suggestion for generic ADTs) - #160069 (Update Rust crate tracing-subscriber to v0.3.23 [SECURITY]) - #160071 (sanitize_standard_fds: clarify macos comment) - #160076 (use unstable features when updating dependencies) - #160092 (miri ui tests: don't run native tests on stage 0) - #160093 (Switch cargo assignments to weihanglo) - #160094 (Update assignment for docs)
…uwer Rollup of 21 pull requests Successful merges: - rust-lang/rust#159990 (Many "predicate"-to-"clause" renamings) - rust-lang/rust#159665 (Replace most `Ty::new_fn_def` calls with `type_of` queries directly) - rust-lang/rust#159687 (rustdoc: Set tracing max_level_info when debug-logging is false) - rust-lang/rust#160057 (refactor(mir-transform): Calculate optimization status inside `run_passes_inner`) - rust-lang/rust#160060 (codegen: skip stores for entirely-uninit constant aggregate fields, attempt rust-lang/rust#2) - rust-lang/rust#160063 (Fix ICE when dumping the dep graph with the parallel frontend) - rust-lang/rust#160065 (Distinguish the dep-graph index space from the live node count) - rust-lang/rust#158038 (Split register_tool into register_attribute_tool and register_lint_tool) - rust-lang/rust#159776 (remove const hack in alloc) - rust-lang/rust#159978 (run intrinsic-test by default on x86_64-gnu) - rust-lang/rust#160008 (Avoid stale closure recovery state across statements) - rust-lang/rust#160027 (Add regression test for rust-lang/rust#132767) - rust-lang/rust#160030 (Update `browser-ui-test` version to `0.25.0`) - rust-lang/rust#160046 (Improve consistency of attribute error messages (part 2)) - rust-lang/rust#160056 (Fix associated function suggestion for generic ADTs) - rust-lang/rust#160069 (Update Rust crate tracing-subscriber to v0.3.23 [SECURITY]) - rust-lang/rust#160071 (sanitize_standard_fds: clarify macos comment) - rust-lang/rust#160076 (use unstable features when updating dependencies) - rust-lang/rust#160092 (miri ui tests: don't run native tests on stage 0) - rust-lang/rust#160093 (Switch cargo assignments to weihanglo) - rust-lang/rust#160094 (Update assignment for docs)
View all comments
Tracking issue: #66079
This implements the attribute parsing part of RFC 3808. The resolution part is yet to be implemented.
cc @jyn514